<?php


namespace app\api\controller;

use app\common\model\Member;
use app\common\model\MemberCollect;
use app\common\model\MobileCode;
use app\validate\Check;
use app\validate\UserCheck;
use library\Controller;
use think\Db;
use think\Request;
class User extends Base
{

    //我的收藏
    public function my_collect(MemberCollect $memberCollect, Request $request, Check $check){
        try {
            $param = $request->param();
            $result = $check->scene('my_collect')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $memberCollect->lists($param);
                if ($list) {
                    return $this->api_result(200, '请求成功', $list);
                } else {
                    return $this->api_result(400, '请求失败', []);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //个人中心
    public function user_info(Member $member, Request $request, Check $check){
        try {
            $param = $request->param();
            $result = $check->scene('user_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->user_info($param);

                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //申请合作
    public function apply_cooperate(Member $member, Request $request, UserCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('apply_cooperate')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->apply_cooperate($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //合作回显
    public function find_cooperate(Member $member, Request $request, Check $check){
        try {
            $param = $request->param();
            $result = $check->scene('user_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->find_cooperate($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //余额明细
    public function user_money_log(Member $member, Request $request, Check $check){
        try {
            $param = $request->param();
            $result = $check->scene('user_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->user_money_log($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    public function user_money_log2(Request $request){
        $param = $request->param();
        $list = Db::table("store_fenyong")->field("jiesuan_time")->where("is_jiesuan=1")->group("jiesuan_time")->select();
        foreach ($list as $k =>&$v){
            $v['create_at'] = $v['jiesuan_time'];
            $v['amount'] =  sprintf('%.2f',Db::table("store_fenyong")->where(['user_id'=>$param['user_id']])->where("is_jiesuan=1")->where(['jiesuan_time'=>$v['jiesuan_time']])->sum('amount'));
        }


        return $this->api_result(200, '请求成功', $list);
    }


    //反馈
    public function feedback(Member $member, Request $request, UserCheck $check){
        try {
            $param = $request->param();
            $result = $check->scene('feedback')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->feedback($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //反馈记录
    public function feedback_list(Member $member, Request $request, Check $check){
        try {
            $param = $request->param();
            $result = $check->scene('user_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->feedback_list($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //余额提现
    public function withdrawal(Member $member, Request $request, UserCheck $check){
        Db::startTrans();
        try {
            $param = $request->param();
            $result = $check->scene('withdrawal')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $msg = $member->withdrawal($param);
                if($msg==1){
                    Db::commit();
                    return $this->api_result(200, '请求成功', $msg);
                }else{
                    return $this->api_result(200, $msg, []);
                }
            }
        } catch (\Exception $e) {
            Db::rollback();
            return $this->api_result(400, '请求失败', []);
        }
    }



}